<template>
    <div>
        <!-- 搜索栏 -->
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="用户信息">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.user"
                            placeholder="请输入用户编号/昵称/手机号"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="充值单号">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.order_sn"
                            placeholder="请输入充值单号"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="支付方式">
                    <el-select
                        v-model="queryParams.pay_way"
                        class="w-[250px]"
                        placeholder="请选择"
                    >
                        <el-option value="" label="全部"/>
                        <el-option
                            v-for="(value, key) in pager.extend?.pay_way"
                            :key="key"
                            :label="value"
                            :value="key"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="支付状态">
                    <el-select
                        v-model="queryParams.pay_status"
                        class="w-[250px]"
                        placeholder="请选择"
                    >
                        <el-option value="" label="全部"/>
                        <el-option
                            v-for="(value, key) in pager.extend?.pay_status"
                            :key="key"
                            :label="value"
                            :value="key"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="下单时间">
                    <date-picker
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPaging">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格栏 -->
        <el-card v-loading="pager.loading" class="!border-none" shadow="never">
            <el-table :data="pager.lists" size="large">
                <el-table-column label="充值单号" prop="order_sn" width="200" />
                <el-table-column label="用户信息" min-width="120">
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <el-avatar :size="36" :src="row.user?.avatar"/>
                            <div class="ml-2">
                                <p>{{ row?.user?.nickname }}</p>
                                <p class="text-xs text-tx-placeholder">{{ row?.user?.sn }}</p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="套餐名称" prop="pack_name" min-width="120" />
                <el-table-column label="实付金额" prop="paid_amount" min-width="100" />
                <el-table-column label="支付方式" prop="pay_way" min-width="100" />
                <el-table-column label="支付状态" prop="pay_status" min-width="100">
                    <template #default="{ row }">
                        <el-tag type="success" v-if="row.pay_status">已支付</el-tag>
                        <el-tag type="info" v-else>未支付</el-tag>
                        <p v-if="row.refund_status === 1" class="ml-2 text-warning text-xs">退款中</p>
                        <p v-if="row.refund_status === 2" class="ml-2 text-warning text-xs">已退款</p>
                        <p v-if="row.refund_status === 3" class="ml-1 text-error text-xs">退款失败</p>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="175" />
                <el-table-column label="支付时间" prop="pay_time" min-width="175" />
                <el-table-column label="操作" width="130" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            type="primary"
                            link
                            @click="handleDetail(row)"
                        >
                            详情
                        </el-button>
                        <el-button
                            v-if="row.refund_status === 0"
                            type="danger"
                            link
                            @click="handleRefund(row.id)"
                        >
                            退款
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <paging v-model="pager" @change="queryLists" />
            </div>
        </el-card>

        <!-- 详情弹窗 -->
        <popup
            :show="showPop"
            width="600px"
            title="订单详情"
            confirmButtonText=""
            cancelButtonText=""
            @close="showPop = false"
        >
            <div class="p-6 pt-2">
                <el-form label-width="80px" label-position="left">
                    <el-form-item label="订单编号：" style="margin-bottom: 5px;">
                        {{ details.order_sn }}
                    </el-form-item>
                    <el-form-item label="套餐名称：" style="margin-bottom: 5px;">
                        {{ details.pack_name }}
                    </el-form-item>
                    <el-form-item label="支付方式：" style="margin-bottom: 5px;">
                        {{ details.pay_way }}
                    </el-form-item>
                    <el-form-item label="实付金额：" style="margin-bottom: 5px;">
                        {{ details.paid_amount }}
                    </el-form-item>
                    <el-form-item label="支付时间：" style="margin-bottom: 5px;">
                        {{ details.pay_time }}
                    </el-form-item>
                    <el-form-item label="下单时间：" style="margin-bottom: 5px;">
                        {{ details.create_time }}
                    </el-form-item>
                    <el-divider content-position="left">套餐内容</el-divider>
                    <el-form-item v-if="details.points_num" label="积分数量：" style="margin-bottom: 5px;">
                        <span class="text-success font-bold">+{{ details.points_num }}</span>
                    </el-form-item>
                    <el-form-item v-if="details.train_num" label="训练组数：" style="margin-bottom: 5px;">
                        <span class="text-success font-bold">+{{ details.train_num }}</span>
                    </el-form-item>
                    <el-form-item v-if="details.robot_num" label="智能体数：" style="margin-bottom: 5px;">
                        <span class="text-success font-bold">+{{ details.robot_num }}</span>
                    </el-form-item>
                    <el-form-item v-if="details.know_num" label="知识库数：" style="margin-bottom: 5px;">
                        <span class="text-success font-bold">+{{ details.know_num }}</span>
                    </el-form-item>
                </el-form>
            </div>
        </popup>
    </div>
</template>

<script setup lang="ts">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import rechargeApi from '@/api/finance/recharge'

interface rechargeOrderType {
    id: number,
    order_sn: string
    pack_name: string,
    points_num: number,
    train_num: number,
    robot_num: number,
    know_num: number,
    paid_amount: number,
    pay_way: string,
    pay_status: number,
    create_time: string,
    pay_time: string,
    user: any
}

// 详情数据
const showPop = ref<boolean>(false)
const details = ref({} as rechargeOrderType)

// 查询参数
const queryParams = reactive({
    user: '',
    order_sn: '',
    pay_way: '',
    pay_status: '',
    start_time: '',
    end_time: ''
})

// 分页查询
const { pager, queryLists, resetParams, resetPaging } = usePaging({
    fetchFun: rechargeApi.lists,
    params: queryParams
})

/**
 * 处理详情
 */
const handleDetail = (row: rechargeOrderType) => {
    details.value = row
    showPop.value = true
}

/**
 * 发起退款
 */
const handleRefund = async (id: number) => {
    feedback.confirm('确定要退款该订单吗?').then(async () => {
        await rechargeApi.refund(id)
        await queryLists()
        feedback.msgSuccess('发起成功')
    }).catch(() => {})
}

onMounted(async () => {
    await queryLists()
})
</script>
